package com.nisco.ampf;

import com.nisco.ampf.db.DBAdapter;
import com.nisco.ampf.db.LoginInfo;
import com.nisco.ampf.util.WebServerConn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
	private EditText nameEdit;
	private EditText passEdit;
	private TextView loginErrorView;
	private Button loginBtn;
	private Button resetBtn;

	private WebServerConn serverConn;
	private String result;
	private String[] resultList;
	private String name;
	private String password;
	DBAdapter dbAdapter;
	LoginInfo[] loginInfos;
	LoginInfo loginInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		nameEdit = (EditText) findViewById(R.id.nameEdit);
		passEdit = (EditText) findViewById(R.id.passEdit);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		resetBtn = (Button) findViewById(R.id.resetBtn);
		loginErrorView = (TextView) findViewById(R.id.loginErrorView);
		serverConn = new WebServerConn();
		// 初始化数据库
		dbAdapter = new DBAdapter(LoginActivity.this);
		dbAdapter.Open();
		// 初始化登录信息，用来获取数据库数据
		loginInfo = new LoginInfo();
		loginInfos = dbAdapter.queryAllData();
		if (loginInfos != null) {
			nameEdit.setText(loginInfos[0].userName);
			passEdit.setText(loginInfos[0].passWord);
		}
		loginBtn.setOnClickListener(new btnListener());
		resetBtn.setOnClickListener(new btnListener());
	}

	class btnListener implements OnClickListener {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.loginBtn:

				name = nameEdit.getText().toString().toUpperCase();
				password = passEdit.getText().toString();
				if (loginInfos == null) {

					loginInfo.userName = name;
					loginInfo.passWord = password;
					dbAdapter.insert(loginInfo);
					
				}
				if (name.length() > 0 && password.length() > 0) {
					result = serverConn.Login(name, password);
					resultList = result.split(":");
					if (resultList[0].equals("Y")) {
						Intent intent = new Intent(LoginActivity.this,
								MenuActivity.class);
						intent.putExtra("result", result);
						intent.putExtra("username", name);
						dbAdapter.close();
						startActivity(intent);
						// 登录成功后 结束登陆界面
						LoginActivity.this.finish();
					} else {
						// loginErrorView.setVisibility(View.VISIBLE);
						loginErrorView.setText(resultList[0] + "用户名或密码错误！");
					}
				} else if (name.length() == 0) {
					// loginErrorView.setVisibility(View.VISIBLE);
					loginErrorView.setText("工号不能为空！");
					nameEdit.setFocusable(true);
				} else if (password.length() == 0) {
					// loginErrorView.setVisibility(View.VISIBLE);
					loginErrorView.setText("密码不能为空！");
					passEdit.setFocusable(true);
				}
				break;
			case R.id.resetBtn:
				nameEdit.setText("");
				passEdit.setText("");
				loginErrorView.setText("");
				break;
			default:
				break;
			}

		}

	}
}
